//
//  UIControl+ABCRepeateClick.m
//  ABCRepeateClick
//
//  Created by luoshizhen on 2024/7/26.
//

#import "UIControl+ABCRepeateClick.h"
#import <objc/runtime.h>
#import "ABCRepeatClickManager.h"
@implementation UIControl (ABCRepeateClick)

- (NSTimeInterval)repeateTime
{
    return [objc_getAssociatedObject(self, _cmd) doubleValue];
}
- (void)setRepeateTime:(NSTimeInterval)repeateTime
{
    objc_setAssociatedObject(self, @selector(repeateTime), @(repeateTime), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    
}
- (void)setDisablePrevent:(BOOL)disablePrevent{
    objc_setAssociatedObject(self, @selector(disablePrevent), @(disablePrevent), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (BOOL)disablePrevent{
    return [objc_getAssociatedObject(self, _cmd) boolValue];
}
//YES 不允许点击   NO 允许点击
- (void)setIsIgnoreEvent:(BOOL)isIgnoreEvent{
    objc_setAssociatedObject(self, @selector(isIgnoreEvent), @(isIgnoreEvent), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (BOOL)isIgnoreEvent{
    return [objc_getAssociatedObject(self, _cmd) boolValue];
}
+(void)openPreventRepeatClick{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        SEL selA = @selector(sendAction:to:forEvent:);
        SEL selB = @selector(mySendAction:to:forEvent:);
        Method methodA = class_getInstanceMethod(self, selA);
        Method methodB = class_getInstanceMethod(self, selB);
        BOOL isAdd = class_addMethod(self, selA, method_getImplementation(methodB), method_getTypeEncoding(methodB));
        if (isAdd) {
            class_replaceMethod(self, selB, method_getImplementation(methodA), method_getTypeEncoding(methodA));
        }else{
            method_exchangeImplementations(methodA, methodB);
        }
    });
}
//+ (void)load{
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        SEL selA = @selector(sendAction:to:forEvent:);
//        SEL selB = @selector(mySendAction:to:forEvent:);
//        Method methodA = class_getInstanceMethod(self, selA);
//        Method methodB = class_getInstanceMethod(self, selB);
//        BOOL isAdd = class_addMethod(self, selA, method_getImplementation(methodB), method_getTypeEncoding(methodB));
//        if (isAdd) {
//            class_replaceMethod(self, selB, method_getImplementation(methodA), method_getTypeEncoding(methodA));
//        }else{
//            method_exchangeImplementations(methodA, methodB);
//        }
//    });
//}

- (void)mySendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event
{
    if (self.disablePrevent == YES) {
        [self mySendAction:action to:target forEvent:event];
        return;
    }
    if ([NSStringFromClass(self.class) isEqualToString:@"UIButton"]) {
        
        self.repeateTime =self.repeateTime ==0 ?[ABCRepeatClickManager shareInstance].defualtRepeateTime:self.repeateTime;
        if (self.isIgnoreEvent){
            return;
        }else if (self.repeateTime > 0){
            [self performSelector:@selector(resetState) withObject:nil afterDelay:self.repeateTime];
        }
    }
    self.isIgnoreEvent = YES;
    [self mySendAction:action to:target forEvent:event];
}
- (void)resetState{
    [self setIsIgnoreEvent:NO];
}
@end
